using UnityEngine;
using System.Collections;

public class Item : MonoBehaviour {
	
	public enum ITEM_TYPE{
		NULL = 0,
		RED = 1,
		PURPLE = 2,
		GREED = 3,
		BLUE = 4,
		PINK = 5,
	}
	public static int ITEM_TYPE_NUM = 5;
	private ITEM_TYPE itemType = ITEM_TYPE.RED;
	private int row;
	private int column;
	public UISprite icon;
	public Animation anim;
	private GameController gameController;
	
	public bool isPlayer = false;
	public bool IsAnimation  = false;
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	public void SetItemType(ITEM_TYPE type,int tRow,int tColumn){
		itemType = type;
		row = tRow;
		column = tColumn;
		gameObject.name = "item"+row+"_"+column;
		Refresh();
	}
	public void SetItemType(ITEM_TYPE type){
		itemType = type;
		Refresh();
	}
	public int GetRow(){
		return row;
	}
	public int GetColumn(){
		return column;
	}
	public ITEM_TYPE GetItemType(){
		return itemType;
	}
	public IEnumerator HoldDown(){
		GameObject obj = NGUITools.AddChild(JhDragDropRoot.root.gameObject,gameObject);
		obj.transform.position = transform.position;
		BoxCollider box = obj.GetComponent<BoxCollider>();
		if(box != null){
			box.enabled = false;
		}
		Vector3 pos = obj.transform.localPosition;
		pos.z = 0f;
		obj.transform.localPosition = pos;
		itemType = ITEM_TYPE.NULL;
		Refresh();
		while(obj.transform.localPosition.y > gameController.player.transform.localPosition.y + 150){
			obj.transform.localPosition = new Vector3(obj.transform.localPosition.x,obj.transform.localPosition.y - Time.deltaTime * 4000,obj.transform.localPosition.z);
			yield return 1;
		}
		NGUITools.Destroy(obj);
		
	}
//	void OnDrop(GameObject go){
//		
//		
//	}
	public void HoldUpAll(){
		for(int i = 0;i < gameController.player.itemCount;i++){
			StartCoroutine(HoldUp(i));
		}
	}
	public IEnumerator HoldUp(int index){
		GameObject obj = NGUITools.AddChild(JhDragDropRoot.root.gameObject,gameObject);
		obj.GetComponent<Item>().SetItemType(gameController.player.item.GetItemType());
		BoxCollider box = obj.GetComponent<BoxCollider>();
		if(box != null){
			box.enabled = false;
		}
		obj.transform.position = transform.position;
		obj.transform.position = new Vector3(transform.position.x,gameController.player.transform.position.y,0);
		obj.transform.localPosition = new Vector3(obj.transform.localPosition.x,obj.transform.localPosition.y + 150,0);
		bool result = true;
		while(result){
			Item item = gameController.GetNullItemByColumn(column,index);
			
			obj.transform.localPosition = new Vector3(obj.transform.localPosition.x,obj.transform.localPosition.y + Time.deltaTime * 4000,obj.transform.localPosition.z);
				
			if(obj.transform.position.y > item.transform.position.y){
//				obj.transform.localPosition.y = item.transform.localPosition.y;
				result = false;
				item.SetItemType(obj.GetComponent<Item>().GetItemType());
			}
			yield return 1;
		}
		NGUITools.Destroy(obj);
		gameController.StartClearUp();
		gameController.Refresh();
	}
	public IEnumerator ClearUp(){
//		Debug.Log("ClearUp");
//		Debug.Log("Row="+ row);
//		Debug.Log("Column="+ column);
		
		anim.Play("ClearUp");
		IsAnimation  = true;
		while(anim.isPlaying){
			yield return new WaitForSeconds(0.1f);
		}
		IsAnimation  = false; 
		itemType = ITEM_TYPE.NULL;
		Refresh();
	}
	public bool IsNull(){
		if(itemType == ITEM_TYPE.NULL){
			return true;
		}else{
			return false;
		}
	}
	public void Refresh(){
		if((int)itemType > 0){
			NGUITools.SetActiveChildren(gameObject,true);
			icon.spriteName = ((int)itemType).ToString();
		}else{
			NGUITools.SetActiveChildren(gameObject,false);
		}
	}
	public void SetGameController(GameController game){
		this.gameController = game;
	}
	public bool IsCanDrag(){
		if(IsNull()){
			return false;
		}
		
		if(isPlayer){
			return true;
		}
		for(int i = row + 1;i < GameController.ROW_NUM;i++){
			if(!gameController.itemList[i,column].IsNull()){
				return false;
			}
		}
		return true;
	}
	void OnDrop(GameObject go){
		GameHeroItem player = go.GetComponent<GameHeroItem>();
		if(player != null){
			Item itemTmp = player.item;
			if(itemTmp.isPlayer && !isPlayer){
//				if(!IsNull() || GetItemType() == itemTmp.GetItemType()){
//					Player player = gameController.GetPlayer();
//					int nullRow = gameController.GetNullRowByColumn(GetColumn());
//					for(int i = 0;i < player.itemCount;i++){
//						gameController.itemList[nullRow + i,GetColumn()].SetItemType(itemTmp.GetItemType());
//					}
					
//					StartCoroutine();
					HoldUpAll();
					player.Clear();
//				}
			}
		}else{
			float orgY = go.GetComponent<JhDragDropItem>().mOrgPos.y;
			Debug.Log (orgY);
			if(go != null && go.GetComponent<JhDragDropItem>() != null && go.GetComponent<JhDragDropItem>().newGameObject != null){
				Debug.Log (go.GetComponent<JhDragDropItem>().newGameObject.transform.localPosition.y);
				if(go.GetComponent<JhDragDropItem>().newGameObject.transform.localPosition.y < orgY){
					gameController.GetPlayer().OnDrop(go);
				}
			}
//			Item itemTmp = go.GetComponent<Item>();
//			if(go.transform.localPosition.y < itemTmp.GetComponent<JhDragDropItem>().mOrgPos.y){
//				if(itemTmp.IsNull() || !itemTmp.IsCanDrag() || itemTmp.isPlayer || gameController.GetPlayer().itemCount >= GameController.Player_Max_Count){
//					return ;
//				}
//				if(gameController.GetPlayer().item.IsNull() || gameController.GetPlayer().item.GetItemType() == itemTmp.GetItemType()){
//					gameController.GetPlayer().item.SetItemType(itemTmp.GetItemType());
//					itemCount++;
//					StartCoroutine(gameController.GetPlayer().ItemHoldDown(gameController.itemList[itemTmp.GetRow(),itemTmp.GetColumn()]));
//					Transform tran = gameController.itemList[itemTmp.GetRow(),itemTmp.GetColumn()].transform;
//					playerSprite.transform.position = new Vector3(tran.position.x,playerSprite.transform.position.y,playerSprite.transform.position.z);
//		//			yield return StartCoroutine(gameController.StartClearUp());
//				}
//			}
		}
	}
}
